package com.googlecode.websword.rest.service.convert.simple;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.commons.lang.StringUtils;

import com.googlecode.websword.rest.utils.NumberUtils;


public class NumberParamConvert extends AbstractRestParamConvert {
    @Override
    public Object convert(String key, Class<?> clz) {
        if (StringUtils.isBlank(key))
            throw new IllegalArgumentException("Cannot convert [ null or '' ] to target class [" + clz.getName() + "]");
        if (clz.equals(Byte.class) || clz.equals(byte.class)) {
            return NumberUtils.parseNumber(key, Byte.class);
        } else if (clz.equals(Short.class) || clz.equals(short.class)) {
            return NumberUtils.parseNumber(key, Short.class);
        } else if (clz.equals(Integer.class) || clz.equals(int.class)) {
            return NumberUtils.parseNumber(key, Integer.class);
        } else if (clz.equals(Long.class) || clz.equals(long.class)) {
            return NumberUtils.parseNumber(key, Long.class);
        } else if (clz.equals(BigInteger.class)) {
            return NumberUtils.parseNumber(key, BigInteger.class);
        } else if (clz.equals(Float.class) || clz.equals(float.class)) {
            return NumberUtils.parseNumber(key, Float.class);
        } else if (clz.equals(Double.class) || clz.equals(double.class)) {
            return NumberUtils.parseNumber(key, Double.class);
        } else if (clz.equals(BigDecimal.class) || clz.equals(Number.class)) {
            return NumberUtils.parseNumber(key, BigDecimal.class);
        } else {
            throw new IllegalArgumentException("Cannot convert String [" + key + "] to target class [" + clz.getName() + "]");
        }
    }
}
